package com.huiquan.section.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.foundation.constant.BusinessContants;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.GetListUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.section.dao.SectionDao;
import com.huiquan.section.domain.Section;

@Service
public class SectionService extends BaseService {

	@Autowired
	private SectionDao sectionDao;

	public ModelAndView list(String startStr, String nameKey, String exactSearchFlag, String sexKey, int sexAgeFlag,
			String countPerPageStr) {

		Map<String, Object> param = new HashMap<>();
		if (nameKey != null && !nameKey.isEmpty()) {
			if (exactSearchFlag != null && exactSearchFlag.equals("1")) {
				param.put("name", nameKey);
			} else {
				param.put("name", "%" + nameKey + "%");
			}
		}
		if (sexKey != null && !sexKey.isEmpty()) {
			param.put("sex", sexKey);
		}
		param.put("sexAgeFlag", sexAgeFlag);
		param.put("disable", 0);

		int totalSize = sectionDao.retrieveSize(param);

		Map<String, Integer> pageNo;
		if (countPerPageStr != null && !countPerPageStr.isEmpty()) {
			int countPerPage = Integer.parseInt(countPerPageStr);
			pageNo = GetListUtil.getPageNoParam(totalSize, startStr, countPerPage);
			param.put("countPerPage", countPerPage);
		} else {
			pageNo = GetListUtil.getPageNoParam(totalSize, startStr);
		}
		int endPage = pageNo.get("endPage");
		int start = pageNo.get("start");
		int startIndex = pageNo.get("startIndex");

		List<Section> list = new ArrayList<>();

		if (startIndex < totalSize) {
			param.put("startIndex", startIndex);
			list = sectionDao.retrieveList(param);
		}

		Map<String, String> showSearch = new LinkedHashMap<>();
		showSearch.put("nameKey", "名称");

		Map<String, Object> map = GetListUtil.getReturnMap2(totalSize, start, startIndex, endPage, list, null);
		map.put("showSearch", showSearch);
		map.put("ageArray", BusinessContants.ageArray);

		return new ModelAndView("section/index", map);
	}

	public ReturnData delete(Long id) {
		Section section = new Section();
		section.setId(id);
		sectionDao.delete(section);

		return ReturnUtil.success();
	}

	public ReturnData setSex(String id, String sex, User user) {
		sectionDao.setSex(id, sex, user);
		return ReturnUtil.success();
	}

	public ReturnData setAge(String id, String age, Boolean flag, User user) {
		Section section = sectionDao.retrieveObjectById(Long.parseLong(id));

		if (flag) {
			// flag为true则添加该年龄段
			if (section.getAge() == null || section.getAge().indexOf(age) < 0) {
				age = (section.getAge() == null ? "" : section.getAge()) + age + ";";
			} else {
				age = section.getAge();
			}
		} else {
			// flag为false则去除该年龄段
			if (section.getAge() != null) {
				age = section.getAge().replace(age + ";", "");
			} else {
				age = "";
			}
		}
		sectionDao.setAge(id, age, user);

		return ReturnUtil.success();
	}

	public ReturnData submitSexAge(String ids, User user) {
		if (ids != null && !ids.isEmpty()) {
			if (ids.lastIndexOf(",") == ids.length() - 1) {
				ids = ids.substring(0, ids.length() - 1);
			}
			sectionDao.submitSexAge(ids, user);
		}
		return ReturnUtil.success();
	}
}
